import axios from "@/utils/axios";
const OSS = require('ali-oss');
const client = new OSS({
  region:"oss-cn-beijing",
  accessKeyId: 'LTAI5tEzd8grjeQbc13GcP4a',
  accessKeySecret: 'j4Cg1EJ09WaiKSdjVcqASuA5EVz1ay',
  bucket:"back-yard"
})
export function getBanner(){
  return axios.get('/banner');
}
export function login(data){
  return axios.post('/login',data,{
    withCredentials:true
  });
}
export function checkLogin(){
  return axios.get('/checkLogin',{
    withCredentials:true
  })
}
export function register(data){
  return axios.post('/register',data,{
    withCredentials:true
  });
}
export function getHobby(){
  return axios.get('/hobby');
}

export function getUserDataByName(username){
  return axios.post('/getUserDataByName',{
    userName:username
  },{
    withCredentials:true
  })
}
export function getUserData(){
  return checkLogin().then(res=>{
    return axios.post('/getUserData',{
      id:res.userInfo._id,
      propertyName:"hobby"
    },{
      withCredentials:true
    })
  })
}
export function updateUserHobby(data){
  return checkLogin().then(res=>{
    axios.post('/growUp',{
      id:res.userInfo._id,
      isNew:false
    })
    return axios.post('/updateUserData',{
      id:res.userInfo._id,
      propertyName:'hobby',
      propertyValue:data
    },{
      withCredentials:true
    })
  })
}
export function updateUserName(data){
  return checkLogin().then(res=>{
    return axios.post('/updateUserData',{
      id:res.userInfo._id,
      propertyName:'userName',
      propertyValue:data
    },{
      withCredentials:true
    })
  })
}
export function updateUserDesc(data){
  return checkLogin().then(res=>{
    return axios.post('/updateUserData',{
      id:res.userInfo._id,
      propertyName:'userDesc',
      propertyValue:data
    },{
      withCredentials:true
    })
  })
}
export async function updateUserImage(data){
  if(data.file?.name.substring(data.file?.name.indexOf('.') + 1) == 'jpg'){
    return checkLogin().then(async res=>{
      let url = '/userData/' + res.userInfo.userName + "/" + res.userInfo.userName + '.jpg'
      await client.put(url,data.file);
      return axios.post('/updateUserData',{
        id:res.userInfo._id,
        propertyName:'userImgUrl',
        propertyValue:url
      },{
        withCredentials:true
      })
    })
  }
  else {
    return {
      message:"上传格式错误",
      status:405
    }
  }
  
}


export function getArticles(){
  return axios.get('/getArticles');
}
export function getUserArticles(){
  return checkLogin().then(res1=>{
    return getArticles().then(res2=>{
      return res2.filter(item=>item.authorId == res1.userInfo._id)
    })
  })
}
export function addArticle(data){
  return checkLogin().then(res=>{
    return getArticles().then(async res2=>{
      let url = '/articleCover/' + res.userInfo._id + res2.length + ".jpg";
      await client.put(url,data.coverUrl.file);
      return axios.post('/addArticle',{
        id:res.userInfo._id,
        title:data.title,
        coverUrl:url,
        tag:data.tag,
        content:data.content
      },{
        withCredentials:true
      })
    })
  })
}
export function delArticles(id){
  return axios.post('/delArticles',{
    id:id
  },{
    withCredentials:true
  })
}


export function getVideos(){
  return axios.get('/getVideos');
}
export function getUserVideos(){
  return checkLogin().then(res1=>{
    return getVideos().then(res2=>{
      return res2.filter(item=>item.authorId == res1.userInfo._id)
    })
  })
}
export function addVideo(data){
  return checkLogin().then(res=>{
    return getVideos().then(async res2=>{
      let videoCoverUrl = '/videos/' + res.userInfo._id + res2.length + ".jpg";
      let videoUrl = '/videos/' + res.userInfo._id + res2.length + ".mp4";
      await client.put(videoCoverUrl,data.videoCover.file);
      await client.put(videoUrl,data.videoFile.file);
      return axios.post('/addVideo',{
        id:res.userInfo._id,
        title:data.title,
        desc:data.desc,
        coverUrl:videoCoverUrl,
        videoUrl:videoUrl,
        tag:data.tag
      },{
        withCredentials:true
      })
    })
  })
}
export function delVideo(id){
  return axios.post('/delVideo',{
    id:id
  },{
    withCredentials:true
  })
}

export function getClub(){
  return axios.get('/getClub');
}
export function addClub(data){
  return checkLogin().then(res1=>{
    return getClub().then(async res2=>{
      let clubCoverUrl = '/clubCover/' + res1.userInfo._id + res2.length + ".jpg";
      await client.put(clubCoverUrl,data.clubCover.file);
      return axios.post('/addClub',{
        id:res1.userInfo._id,
        clubName:data.clubName,
        clubDesc:data.clubDesc,
        clubCover:clubCoverUrl,
        clubTag:data.clubTag
      },{
        withCredentials:true
      })
    })
  })
}
export function getUserClub(){
  return checkLogin().then(res1=>{
    return getClub().then(res2=>{
      return res2.filter(item=>item.authorId == res1.userInfo._id)
    })
  })
}
export function addMember(id){
  return checkLogin().then(res1=>{
    return getClub().then(res2=>{
      let theClub = res2.filter(item=>item._id == id)[0].clubMember;
      let clubMember = theClub.filter(item=>item == res1.userInfo._id).length == 0 ? [...res2.filter(item=>item._id == id)[0].clubMember,res1.userInfo._id] : theClub;
      return axios.post('/addMember',{
        id:id,
        clubMember:clubMember
      })
    })
  })
}
export function appendMember(clubId,memberId){
  return getClub().then(res=>{
    let theClub = res.filter(item=>item._id == clubId)[0].clubMember;
    let clubMember = theClub.filter(item=>item == memberId).length == 0 ? [...res.filter(item=>item._id == clubId)[0].clubMember,memberId] : theClub;
    return axios.post('/addMember',{
      id:clubId,
      clubMember:clubMember
    })
  })
}
export function removeMember(clubId,memberId){
  return getClub().then(res=>{
    let theClub = res.filter(item=>item._id == clubId)[0].clubMember;
    let clubMember = theClub.filter(item=>item == memberId).length != 0 ?  theClub.filter(item=>item != memberId): theClub;
    return axios.post('/addMember',{
      id:clubId,
      clubMember:clubMember
    })
  })
}

export function getActivity(){
  return axios.get('/getActivity');
}
export function addActivity(data){
  return getActivity().then(async res=>{
    let activityUrl = '/activity/' + data.id + res.length + '.jpg';
    await client.put(activityUrl,data.coverUrl.file)
    return axios.post('/addActivity',{
      clubId:data.id,
      coverUrl:activityUrl,
      content:data.content,
      startTime:data.startTime,
      endTime:data.endTime,
      position:data.position,
      positionDetail:data.positionDetail
    })
  })
}